package com.dayaokeji.rhythmschoolstudent.client.common.base.common;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.RequestManager;
import com.dayaokeji.rhythmschoolstudent.glide.GlideUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by Gorden on 2017/9/23.
 */

public abstract class BaseFragment extends Fragment {

    private Unbinder unbinder;
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = onCreate(inflater, container, savedInstanceState);
        if (view != null) {
            unbinder = ButterKnife.bind(this,view);
            return view;
        }else{
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    protected abstract View onCreate(LayoutInflater inflater,ViewGroup container, @Nullable Bundle savedInstanceState);

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }
        if (compositeDisposable != null) {
            compositeDisposable.clear();
        }
    }

    protected void addDisposable(@NonNull Disposable d) {
        compositeDisposable.add(d);
    }

    protected RequestManager withGlide() {
        return GlideUtils.with(this);
    }
}
